# -*- encoding: utf-8 -*-
##############################################################################
#
##############################################################################

from odoo import fields, models, api, _
from odoo.exceptions import UserError, AccessError
from odoo.exceptions import ValidationError


class sale_order_line(models.Model):
    _inherit = 'sale.order.line'

    prepare_done = fields.Boolean(u'已准备', default=0, copy=False)
    picking_prepare_done = fields.Selection([('0', u'O'), ('1', u'安排发货')], u'发货状态',
                                            default='0')
    plan_id = fields.Many2one('sale.mrp.plan', u'已计划', copy=False)
    mrp_plan_order_id = fields.Many2one('sale.mrp.plan.order', related="plan_id.order_id", string=u'计划单',
                                        readonly=True)
    need_date = fields.Date(string=u'订购日期', compute="_compute_sale_dates", store=True)
    ask_for_date = fields.Date(string=u'要求日期', compute="_compute_sale_dates", store=True)
    mrp_reply_date = fields.Date(string=u'生管回复日期', compute="_compute_sale_dates", store=True)
    requested_date = fields.Date(string=u"客户要求日期", compute="_compute_sale_dates", store=True)

    @api.depends("order_id",
                 "order_id.confirmation_date",
                 "order_id.requested_date",
                 "order_id.product_commitment_date",
                 "order_id.requested_date")
    def _compute_sale_dates(self):
        for line in self:
            line.need_date = line.order_id.confirmation_date and \
                                 fields.Date.to_string(fields.Datetime.from_string(line.order_id.confirmation_date).date()) or ""
            line.ask_for_date = line.order_id.requested_date and \
                                fields.Date.to_string(fields.Datetime.from_string(line.order_id.requested_date).date()) or ""
            line.mrp_reply_date = line.order_id.product_commitment_date and \
                                  fields.Date.to_string(fields.Datetime.from_string(line.order_id.product_commitment_date).date()) or ""
            line.requested_date = line.order_id.requested_date and \
                                  fields.Date.to_string(fields.Datetime.from_string(line.order_id.requested_date).date()) or ""